showtime();
function addZero(i) {
	if (i < 10) {
		i = "0" + i;
	}
	return i;
}
function showtime() {
	var nowtime = new Date();
	var endtime = new Date("2018/02/08,00:00:00");
	var lefttime = parseInt((endtime.getTime() - nowtime.getTime()) / 1000);
	var d = parseInt(lefttime / (24 * 60 * 60));
	var h = parseInt(lefttime / (60 * 60) % 24);
	var m = parseInt(lefttime / 60 % 60);
	var s = parseInt(lefttime % 60);
	h = addZero(h);
	m = addZero(m);
	s = addZero(s);
	$('.day').html(d);
	$('.houer').html(h);
	$('.minutes').html(m);
	$('.seconds').html(s);
	if (lefttime <= 0) {
		return;
	}
	setTimeout(showtime, 1000);
}
function login_view() {
	var r = Math.random();
	$('#captcha_img').attr('src', "./captcha?r=" +r );
	localStorage.setItem("r", r);
	$(".login").css("display", "block");
	$("#ping").css("display", "block");
}
function cha_hiden() {
	$("#ping").css("display", "none");
	$(".login").css("display", "none");
}
function login() {
	$.getJSON("./user/login", {
		id_card : $("#id_card").val(),
		password : $("#password").val(),
		captcha : $("#captcha").val(),
		r :localStorage.getItem("r")
	}, function(json) {
		if (json.success) {
			cha_hiden()
			$("#a_name").html(json.success.nick_name);
			$("#a_gang_1").html("|");
			$("#a_gang_2").html("|");
			$("#logoutBtn").html("退出登录");
			$("#a_update").html("修改密码");
			localStorage.setItem("token", json.success.guid);
		} else {
			var r = Math.random();
			$('#captcha_img').attr('src', "./captcha?r=" + r);
			localStorage.setItem("r", r);
			$("#error_text").text(json.error);
		}
	});
}

function isLogin() {
	$.getJSON("./user/info", {
		token : localStorage.getItem("token")
	}, function(json) {
		if (json.success) {
			$(".login").css("display", "none");
			$("#a_name").html(json.success.nick_name);
			$("#a_gang_1").html("|");
			$("#a_gang_2").html("|");
			$("#logoutBtn").html("退出登录");
			$("#a_update").html("修改密码");
			localStorage.setItem("token", json.success.guid);
		} else {
			localStorage.removeItem("token");
			$("#a_name").html("");
			$("#a_gang_1").html("");
			$("#a_gang_2").html("");
			$("#a_update").html("");
			$("#logoutBtn").html("登录");
		}
	});
}
$(document).ready(function() {
	document.getElementById("ping").style.height = document.body.scrollWidth;
	isLogin();
	$("#logoutBtn").click(function() {
		var text = $("#logoutBtn").html();
		if (text == "登录") {
			login_view();
		} else {
			localStorage.removeItem("token");
			$("#a_name").html("");
			$("#a_gang_1").html("");
			$("#a_gang_2").html("");
			$("#a_update").html("");
			$("#logoutBtn").html("登录");
		}
	});
	$("#captcha_img").click(function() {
		var r = Math.random();
		$('#captcha_img').attr('src', "./captcha?r=" + r);
		localStorage.setItem("r", r);
	});
	$("#a_update").click(function() {
		$("#ping").css("display", "block");
		$(".updatePassword").css("display", "block");
	});
	$("#getAuthBtn").click(function() {
		var phone = $("#phone").val();
		var i = 60;
		$.getJSON("./user/sendauthcode", {
			phone : phone
		}, function(json) {
			if(json.success){
			$("#getAuthBtn").css("background-color", "#887378");
			$("#getAuthBtn").attr("disabled", "disabled");
			var i = 60;
			getAuthInterval = setInterval(function() {
				i = i - 1;
				$("#getAuthBtn").text(i + "s重新发送");
				if (i < 1) {
					clearInterval(getAuthInterval);
					$("#getAuthBtn").text("获取验证码");
					$("#getAuthBtn").css("background-color", "#2a89ff");
					$("#getAuthBtn").removeAttr("disabled");
				}
			}, 1000);
				}else if(json.error){
					$("#error_up_text").text(json.error);
			}
		});
	});

	// 键盘回车事件
	$(document).keypress(function(e) {
		var eCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
		if (eCode == 13) {
			if ($(".login").css("display") == 'block') {
				login();
			} else if ($("#updatePassword").css("display") == 'block') {
				updatePasswrod();
			}

		}
	});
});
function list_view() {
	$.getJSON("./user/info", {
		token : localStorage.getItem("token")
	}, function(json) {
		if (json.success) {
			localStorage.setItem("token", json.success.guid);
			document.getElementById("token").value = json.success.guid;
			window.location.href = "./list/buildings";
		} else {
			login_view();
			localStorage.removeItem("token");
		}
	});
}

function updatePasswrod() {
	$.getJSON("./user/update/password", {
		id_card : $("#up_id_card").val(),
		phone : $("#phone").val(),
		password : $("#up_password").val(),
		captcha : $("#up_captcha").val()
	}, function(json) {
		if (json.success) {
			alert("密码修改成功！")
			localStorage.removeItem("token");
			cha_paw_hiden();
			window.location.href = "./index";
		} else {
			$("#error_up_text").text(json.error);
		}
	});
}
function cha_paw_hiden() {
	$(".updatePassword").css("display", "none");
	$("#ping").css("display", "none");
};

